// pages/search/search.js
import { HTTP } from '../../utils/http.js'
let ajax = new HTTP()
const app = getApp()

Page({
    data: {
        searchValue: "",
        placeType: "",
        searchNone: false,
        searchList: []
    },
    // 清空input
    clearInput() {
        this.setData({
            searchValue: "",
            searchList: []
        })
    },
    // search
    onInputTap (event) {
        let value = event.detail.value
        if (this.data.placeType === "live") {
            this._getSearchInfo(value)
        }
        if (this.data.placeType === "apply") {
            this._getSearchApply(value)
        }
        if (this.data.placeType === "fitness") {
            this._getSearchFitness(value)
        }
        if (this.data.placeType === "vision") {
            this._getSearchVideo(value)
        }
        if (this.data.placeType === "news") {
            this._getSearchNews(value)
        }
    },
    onLoad (options) {
        this.setData({
            placeType: options.id
        })
    },
    // 搜索跳转
    onSearchTap (event) {
        this.setData({
            searchValue: "",
            searchList: []
        })
        let index = event.currentTarget.dataset.id
        console.log(index)
        let status = event.currentTarget.dataset.status
        let title = event.currentTarget.dataset.title
        let zbactid = event.currentTarget.dataset.zbactid
        // 从直播页面点击过来
        if (this.data.placeType === "live") {
            if (status === 3) {
                wx.showToast({
                    title: '重播页面还在搭建中哦！',
                    icon: 'none'
                })
                return
                wx.redirectTo({
                    url: '/pages/live/record/record?id=' + index,
                })
            }
            if (status === 2) {
                wx.redirectTo({
                    url: '/pages/live/detail/detail?id=' + index,
                })
            }
            if (status === 1) {
                // wx.showToast({
                //     title: '直播尚未开始，敬请期待',
                //     icon: 'none'
                // })
                // wx.navigateTo({
                //     url: '/pages/live/notice/notice?id=' + zbactid
                // })
                wx.navigateTo({
                    url: '/pages/live/detail/detail?id=' + index
                })
            }
        }
        // 从报名赛事信息过来
        if (this.data.placeType === "apply") {
            wx.redirectTo({
                url: '/pages/find/apply/apply?id=' + index + '&title=' + title
            })
        }
        // 从健身培训过来
        if (this.data.placeType === "fitness") {
            wx.redirectTo({
                url: '/pages/find/fitness/detail/detail?id=' + index + '&title=' + title
            })
        }
        // 从视频专题过来
        if (this.data.placeType === "vision") {
            wx.redirectTo({
                url: '/pages/find/vision/detail/detail?id=' + index + '&title=' + title
            })
        }
        // 从资讯专题过来
        if (this.data.placeType === "news") {
            wx.redirectTo({
                url: '/pages/find/news/detail/detail?id=' + index + '&title=' + title
            })
        }
    },
    // 获取直播地址
    _getSearchInfo (keywords) {
        ajax.request({
            url: "activelist",
            data: {
                keywords: keywords
            },
            success: (res) => {
                if (res.resmsg === "数据为空") {
                    this.setData({
                        searchNone: true
                    })
                }
                if (res.rescode === "0000") {
                    console.log(res.data)
                    this.setData({
                        searchNone: false,
                        searchList: res.data
                    })
                }
            }
        })
    },
    // 搜索报名赛事信息
    _getSearchApply (keywords) {
        ajax.request({
            url: "sportslist",
            data: {
                keywords: keywords
            },
            success: (res) => {
                if (res.resmsg === "数据为空") {
                    this.setData({
                        searchNone: true
                    })
                }
                if (res.rescode === "0000") {
                    this.setData({
                        searchNone: false,
                        searchList: res.data
                    })
                }
            }
        })
    },
    // 健身培训检索
    _getSearchFitness (keywords) {
        ajax.request({
            url: "trainlist",
            data: {
                keywords: keywords
            },
            success: (res) => {
                if (res.resmsg === "数据为空") {
                    this.setData({
                        searchNone: true
                    })
                }
                if (res.rescode === "0000") {
                    this.setData({
                        searchNone: false,
                        searchList: res.data
                    })
                }
            }
        })
    },
    // 视频检索
    _getSearchVideo (keywords) {
        ajax.request({
            url: "videolist",
            data: {
                keywords: keywords
            },
            success: (res) => {
                if (res.resmsg === "数据为空") {
                    this.setData({
                        searchNone: true
                    })
                }
                if (res.rescode === "0000") {
                    this.setData({
                        searchNone: false,
                        searchList: res.data
                    })
                }
            }
        })
    },
    // 资讯检索
    _getSearchNews (keywords) {
        ajax.request({
            url: "articlelist",
            data: {
                keywords: keywords
            },
            success: (res) => {
                if (res.resmsg === "数据为空") {
                    this.setData({
                        searchNone: true
                    })
                }
                if (res.rescode === "0000") {
                    this.setData({
                        searchNone: false,
                        searchList: res.data
                    })
                }
            }
        })
    }
})